#!/bin/bash

systemctl stop firewalld
systemctl disable firewalld
systemctl stop NetworkManager
systemctl disable NetworkManager
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime


#更换为本地源，安装必备的工具

if [ ! -d "/etc/yum.repos.d/repos.bak.d" ]
then
    echo "backup repos"
    mkdir -p /etc/yum.repos.d/repos.bak.d
    mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repos.bak.d/
fi

yum_host=

if [ -z $yum_host ]
then
    echo "config deploy master host"
    exit
fi

echo "use local repos"
echo "[centos7]
name=local centos7
baseurl=http://$yum_host/mirrors/centos7
enabled=1
gpgcheck=0

[cloud]
name=local cloud
baseurl=http://$yum_host/mirrors/cloud
enabled=1
gpgcheck=0
">/etc/yum.repos.d/local.repo

yum clean all
yum makecache

yum -y update
yum -y install wget uuid git vim net-tools bash-completion ntpdate expect chrony
##OpenStack tools 安装
yum install -y python-openstackclient openstack-selinux python2-PyMySQL openstack-utils

#设置时间同步
cp /etc/chrony.conf{,.bak}
# yum_host=192.168.100.254
sed -i 's/0.centos.pool.ntp.org/'$yum_host'/' /etc/chrony.conf
sed -i 's/server 0.centos.pool.ntp.org/#&/' /etc/chrony.conf
sed -i 's/server 1.centos.pool.ntp.org/#&/' /etc/chrony.conf
sed -i 's/server 2.centos.pool.ntp.org/#&/' /etc/chrony.conf
sed -i 's/server 3.centos.pool.ntp.org/#&/' /etc/chrony.conf

systemctl enable chronyd
systemctl start chronyd

#下载hosts文件
wget -O /etc/hosts http://$yum_host/openstack/scripts/hosts

#设置服务器名
#TODO：增加用户输入功能
#hostnamectl set-hostname controller1
echo "set hostname use 'hostnamectl set-hostname <hostname>'"
#设置IP为静态
#TODO：增加用户输入功能
#echo "">>/etc/sysconfig/network-scripts/ifcfg-eth0
echo "please set iface eth0 params"

reboot